package com.ruoyi.business.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.business.domain.Alarmdatahis;
import com.ruoyi.business.service.IAlarmdatahisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.AlarmdatarealMapper;
import com.ruoyi.business.domain.Alarmdatareal;
import com.ruoyi.business.service.IAlarmdatarealService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 报警实时记录Service业务层处理
 *
 * @author DL
 * @date 2025-07-02
 */
@Service
public class AlarmdatarealServiceImpl extends ServiceImpl<AlarmdatarealMapper,Alarmdatareal> implements IAlarmdatarealService
{
    @Autowired
    private AlarmdatarealMapper alarmdatarealMapper;
    @Autowired
    private IAlarmdatahisService alarmdatahisService;

    /**
     * 查询报警实时记录
     *
     * @param ID 报警实时记录主键
     * @return 报警实时记录
     */
    @Override
    public Alarmdatareal selectAlarmdatarealByID(Long ID)
    {
        return alarmdatarealMapper.selectAlarmdatarealByID(ID);
    }

    /**
     * 查询报警实时记录列表
     *
     * @param alarmdatareal 报警实时记录
     * @return 报警实时记录
     */
    @Override
    public List<Alarmdatareal> selectAlarmdatarealList(Alarmdatareal alarmdatareal)
    {
        return alarmdatarealMapper.selectAlarmdatarealList(alarmdatareal);
    }

    /**
     * 新增报警实时记录
     *
     * @param alarmdatareal 报警实时记录
     * @return 结果
     */
    @Override
    public int insertAlarmdatareal(Alarmdatareal alarmdatareal)
    {
        return alarmdatarealMapper.insertAlarmdatareal(alarmdatareal);
    }

    /**
     * 修改报警实时记录
     *
     * @param alarmdatareal 报警实时记录
     * @return 结果
     */
    @Override
    public int updateAlarmdatareal(Alarmdatareal alarmdatareal)
    {
        return alarmdatarealMapper.updateAlarmdatareal(alarmdatareal);
    }

    /**
     * 批量删除报警实时记录
     *
     * @param IDs 需要删除的报警实时记录主键
     * @return 结果
     */
    @Override
    public int deleteAlarmdatarealByIDs(Long[] IDs)
    {
        return alarmdatarealMapper.deleteAlarmdatarealByIDs(IDs);
    }

    /**
     * 删除报警实时记录信息
     *
     * @param ID 报警实时记录主键
     * @return 结果
     */
    @Override
    public int deleteAlarmdatarealByID(Long ID)
    {
        return alarmdatarealMapper.deleteAlarmdatarealByID(ID);
    }

    @Override
    @Transactional
    public boolean restoration(List<Alarmdatareal> alarmdatarealList) {
        Date date = new Date();
        //设置历史数据
        List<Alarmdatahis> alarmdatahis = JSONObject.parseArray(JSONObject.toJSONString(alarmdatarealList), Alarmdatahis.class);
        alarmdatahis.forEach(item -> {
            item.setID(0L);
            item.setRecoveryTime(date);
        });
        //逻辑删除报警信息
        alarmdatarealList.forEach(item -> {
            item.setDeleteFlag(1L);
        });
        this.updateBatchById(alarmdatarealList);
        return alarmdatahisService.saveBatch(alarmdatahis);
    }
}
